package com.feng.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.feng.dao.SentenceDao;
import com.feng.entity.Sentence;
import com.feng.util.CommonUtils;
import com.feng.util.PageUtil;

@Repository
public class SentenceDaoImpl implements SentenceDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public void batchInsert(List<Sentence> setences) {
		final List<Sentence> tmpSentences = setences;
		final java.util.Date creDate = new java.util.Date();
		String sql = "insert into t_sentence (id,title,content,type,credate) values(?,?,?,?,now())";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement pstmt, int index)
					throws SQLException {
				Sentence sentence = tmpSentences.get(index);
				pstmt.setString(1, CommonUtils.getUUID());
				pstmt.setString(2, sentence.getTitle());
				pstmt.setString(3, sentence.getContent());
				pstmt.setString(4, sentence.getType());
				//pstmt.setDate(5,  new java.sql.Date(creDate.getTime()));

			}

			@Override
			public int getBatchSize() {
				 //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
				return tmpSentences.size();
			}
		});
	}


}
